﻿using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections;
using sfsocket.corelib.connection.server;

namespace chat_server
{
    public class ChatServer
    {
        private TcpListener m_tcpListener = null;
        private int m_serverPort;
        private bool m_IsStart = false;
        // private Hashtable m_AllConnections = new Hashtable();

        public bool IsStart
        {
            get { return m_IsStart; }
            set { m_IsStart = value; }
        }

        public ChatServer(int serverPort)
        {
            m_serverPort = serverPort;

        }

        public int Start()
        {
            try
            {
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                m_tcpListener = new TcpListener(localAddr, m_serverPort);
                m_tcpListener.ExclusiveAddressUse = false;
                m_tcpListener.Start();
                Console.WriteLine("Listening on {0}", m_serverPort);
                string msg = "Listening on " + m_serverPort;
                TraceMsg(msg);
                m_IsStart = true;
                RunLoop();
            }
            catch (SocketException ex)
            {
                Console.Write(ex.ErrorCode + ": " + ex.Message);
                Environment.Exit(ex.ErrorCode);

                throw ex;
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                Environment.Exit(-1);
                throw ex;

            }
            finally 
            {
                m_tcpListener.Stop();

            }

            return 0;
        }


        private static void TraceMsg(string msg)
        {
            Console.WriteLine("[{0,3}] - {1} : {2}",
                    Thread.CurrentThread.ManagedThreadId,
                    DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
        }


        public int Stop()
        {
            if (m_IsStart)
            {
                if (m_tcpListener!=null)
                {
                    m_tcpListener.Stop();
                }

                m_IsStart = false;
            }
            
            return 0;
        }
        
        private void RunLoop()
        {
            DateTime startTime, endTime;
            TimeSpan durationTime;

            // run forevers
            for (; ; )
            {
                TcpClient tcpClient = null;

                try
                {
                    tcpClient = m_tcpListener.AcceptTcpClient();
                    ChatServerConnection server_con = new ChatServerConnection(tcpClient);
                    startTime = DateTime.Now;
                }
                catch (System.Exception e)
                {
                    TraceMsg(e.Message);
                    throw e;
                }
                finally
                {
                }

            }
        }
    }
}
